package com.byzk.p2p.admin.money.service;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.vo.BorrowTempVo;
import com.byzk.p2p.admin.money.dao.BorrowPaymentDetailDao;
import com.byzk.p2p.admin.money.dto.BorrowPaymentDetail;
import com.byzk.p2p.admin.money.vo.BorrowPaymentDetailVo;
import com.byzk.p2p.admin.money.vo.HfBorrowPaymentDetailVo;

/**
 * 借款人还款明细service		suker.zhou
 * @author suker.zhou
 *
 */
@Service
public class BorrowPaymentDetailService {
	
	@Autowired
	private BorrowPaymentDetailDao borrowPaymentDetailDao;

	
	/**
	 * 借款人还款明细新增修改
	 * @param fundRecord
	 * @return
	 */
	public Integer saveOrUpdate(BorrowPaymentDetail record){
		if(record.getId()==null){
			return borrowPaymentDetailDao.insert(record);
		}else{
			return borrowPaymentDetailDao.update(record);
		}
	}
	
	/**
	 * 借款人还款明细记录
	 * @param limit
	 * @param index
	 * @param record
	 * @return
	 */
	public PageInfo<BorrowPaymentDetailVo> list(BorrowPaymentDetailVo vo){
		int count=borrowPaymentDetailDao.count(vo);
		List<BorrowPaymentDetailVo> recordList=borrowPaymentDetailDao.list(vo);
		PageInfo<BorrowPaymentDetailVo> page = new PageInfo<BorrowPaymentDetailVo>(count,vo.getIndex(), vo.getLimit());
		page.setPageList(recordList);
		return page;
	}
	
	/**
	 * 借款人还款明细详情
	 * @param id
	 * @return
	 */
	public BorrowPaymentDetailVo detail(Integer id){
		BorrowPaymentDetailVo vo=borrowPaymentDetailDao.selectByPrimaryKey(id);
		return vo;
	}
	
	public List<BorrowPaymentDetailVo> detailByBorrowIdAndUserId(Integer tenderId,Integer borrowId,Integer userId){
		return borrowPaymentDetailDao.detailByBorrowIdAndUserId(tenderId,borrowId,userId);
	}
	
	public List<BorrowPaymentDetailVo> borrowDetail(Integer borrowId,Integer userId){
		return borrowPaymentDetailDao.borrowDetail(borrowId, userId);
	}
	
	/**
	 * 根据借款标获取所有还款明细详情 
	 * @param borrowId 借款标id
	 * @return
	 */
	public List<BorrowPaymentDetail> getDetailByBorrowId(Integer borrowId){
		return borrowPaymentDetailDao.selectByBorrowId(borrowId);
	}
	public  List<BorrowPaymentDetail> selectDetailByBorrowId(Integer id,Integer index){
		return borrowPaymentDetailDao.getByBorrowId(id,index);
	}
	
	public BorrowPaymentDetailVo selectBorrowDetail(Integer borrowId,Integer userId,Integer pid){
		return borrowPaymentDetailDao.getBorrowDetail(borrowId, userId, pid);
	}
	
	public Double selectFeeCount(Integer id){
		return borrowPaymentDetailDao.feeCount(id);
	}
	
	public List<BorrowPaymentDetailVo> getByBorrowIdAndIndex(Integer bid,Integer index){
		return borrowPaymentDetailDao.selectByBorrowIdAndIndex(bid, index);
	}
	
	public List<HfBorrowPaymentDetailVo> selectByPaymentId(Integer paumentId){
		return borrowPaymentDetailDao.selectByPaymentId(paumentId);
	}
	
	/*
	 * 通过标id和投资id查询已完成的还款
	 */
	public List<BorrowPaymentDetail> getDetailListByBorrowIdAndTenderId(Integer borrowId,Integer tenderId){
		return borrowPaymentDetailDao.getDetailListByBorrowIdAndTenderId(borrowId, tenderId);
	}

	public List<BorrowPaymentDetailVo> lastAwaitBorrow(Integer userId,Integer num) {
		return borrowPaymentDetailDao.lastAwaitBorrow(userId,num);
	}

	public List<BorrowPaymentDetail> selectByPidAndIndex(Integer id) {
		return borrowPaymentDetailDao.selectByPidAndIndex(id);
	}
	
	public List<BorrowPaymentDetailVo> lastAwaitChangeBorrow(Integer userId,Integer num) {
		return borrowPaymentDetailDao.lastAwaitChangeBorrow(userId,num);
	}

	public List<BorrowPaymentDetail> selectDetailByPaymentId(Integer paymentId){
		return borrowPaymentDetailDao.selectDetailByPaymentId(paymentId);
	}
	
	/**
	 * 还款记录
	 * @param vo
	 * @return
	 */
	public PageInfo<BorrowPaymentDetailVo> repaymentList(BorrowPaymentDetailVo vo) {
		
		int count = borrowPaymentDetailDao.countRepaymentByUserId(vo) ; 
		
		List<BorrowPaymentDetailVo> listRepayment = borrowPaymentDetailDao.myRepaymentList(vo);
		
		PageInfo<BorrowPaymentDetailVo> page = new PageInfo<BorrowPaymentDetailVo>(count, vo.getIndex(), vo.getLimit()) ; 
		
		page.setPageList(listRepayment);
		
		return page ; 
	}
	
}